package com.example.wallet.utils

import android.content.SharedPreferences
import android.preference.PreferenceManager
import com.example.wallet.global.AppGlobals

class Preference {

    companion object {

        val HOLDER by lazy { Preference() }

        private val PREFERENCES by lazy { PreferenceManager.getDefaultSharedPreferences(AppGlobals.getApplication()) }
    }


    private fun getAppPreference(): SharedPreferences {
        return PREFERENCES
    }

    fun clearAppPreferences() {
        getAppPreference()
            .edit()
            .clear()
            .apply()
    }

    fun setAppFlag(key: String?, flag: Boolean) {
        getAppPreference()
            .edit()
            .putBoolean(key, flag)
            .apply()
    }

    fun setAppString(key: String?, flag: String?) {
        getAppPreference()
            .edit()
            .putString(key, flag)
            .apply()
    }

    fun getAppFlag(key: String?): Boolean {
        return getAppPreference()
            .getBoolean(key, false)
    }

    fun getAppString(key: String?): String? {
        return getAppPreference()
            .getString(key, null)
    }

    /**
     * 添加app自定义的数据
     */
    fun addCustomAppProfile(key: String?, `val`: String?) {
        getAppPreference()
            .edit()
            .putString(key, `val`)
            .apply()
    }

    /**
     * 添加app自定义的数据
     */
    fun addCustomAppProfile(key: Enum<*>, `val`: String?) {
        addCustomAppProfile(key.name, `val`)
    }

    /**
     * 获取app自定义的数据
     */
    fun getCustomAppProfile(key: String?): String? {
        return getAppPreference().getString(key, null)
    }

    /**
     * 获取app自定义的数据
     */
    fun getCustomAppProfile(key: Enum<*>): String? {
        return getCustomAppProfile(key.name)
    }

    /**
     * 移除app自定义的数据
     */
    fun removeCustomAppProfile(key: String?) {
        getAppPreference()
            .edit()
            .remove(key)
            .apply()
    }

    /**
     * 移除app自定义的数据
     */
    fun removeCustomAppProfile(key: Enum<*>) {
        removeCustomAppProfile(key.name)
    }

}